package com.pajk.user.service.login.sms;

import java.util.HashMap;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import com.pajk.user.dal.index.Index;
import com.pajk.user.service.common.metaq.BaseConsumer;
import com.pajk.user.service.common.metaq.MsgObj;
import com.pajk.user.service.common.metaq.TopicEnum;

public class SmsUpPasswordConsumer extends BaseConsumer{
	
	private static final Logger log = LoggerFactory.getLogger(SmsUpPasswordConsumer.class);
	
	public static final String MSG_PHONENUMBER = "mobile";
	
	public static final String MSG_SMSCONTENT = "content";
	
	@Resource
    private Index<String,SmsPasswordManager.SmsInfo> smsInfoIndex;
	
	@Override
	public TopicEnum getTopicEnum() {
		return TopicEnum.USER_SMS_UPSTREAM;
	}

	@Override
	public void doLogErrorConsumeMessage(MsgObj msgs) {
		// TODO Auto-generated method stub
	}

	@Override
	public ConsumeConcurrentlyStatus doConsumeMessage(MsgObj msgs) {
        String message = (String) msgs.getMsgList().get(0);
        HashMap<String, String> msg = (HashMap<String, String>) JSON.parseObject(message, HashMap.class);
        SmsPasswordManager.SmsInfo smsInfo = smsInfoIndex.get(msg.get(MSG_PHONENUMBER));
        if(smsInfo != null) {
            smsInfo.isReceivedUpstream = true;
            smsInfoIndex.put(msg.get(MSG_PHONENUMBER), smsInfo);
        }
        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
	}

}
